package sg.edu.nus.iss.vmcs.system;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import sg.edu.nus.iss.vmcs.util.PropertyException;

/** 
 * @version 1.0
 * @author Low Chen Wei
 */

public class FilePropertyImp implements PropertyImp {

	private Properties prop;
	private String fileName;

	public FilePropertyImp(String fileName) {
		this.fileName = fileName;
	}

	public void init() throws PropertyException {
		prop = new Properties(System.getProperties());

		try {
			FileInputStream stream = new FileInputStream(fileName);
			prop.load(stream);
			stream.close();
		} catch (IOException ioe) {
			throw new PropertyException(ioe);
		}
	}

	public void saveProperty() throws PropertyException {
		try {
			save();
		} catch (IOException ioe) {
			throw new PropertyException(ioe);
		}
	}

	public Object getValue(String id, String attribute) {
		return prop.getProperty(attribute + id);
	}
	
	public Object getValue(String attribute) {
		return prop.getProperty(attribute);
	}

	public void setValue(String id, String attribute, Object value) {
		prop.setProperty(attribute + id, value.toString());
	}

	public void setValue(String attribute, Object value) {
		prop.setProperty(attribute, value.toString());
	}

	private void save() throws IOException {
		FileOutputStream stream = new FileOutputStream(fileName);
		prop.store(stream, "");
		stream.close();
	}

}
